package cn.jobstart.config;



import cn.jobstart.interceptor.LoginInterceptor;
import cn.jobstart.interceptor.OptionsInterceptor;
import cn.jobstart.interceptor.RightsInterceptor;
import cn.jobstart.props.FileConfig;
import cn.jobstart.web.common.token.JwtsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author sunke
 * @Date 2018/8/16 10:32
 * @description
 */
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {


/*    @Value("${file.upload.root}")
    private String filePath;*/


    @Autowired
    private FileConfig fileConfig;


    @Value("${debugRun}")
    private boolean debug;
    @Value("${rightsDebug}")
    private boolean rightsDebug;


    @Resource(name = "optionsInterceptor")
    private OptionsInterceptor optionsInterceptor;

    @Resource(name = "loginInterceptor")
    private LoginInterceptor loginInterceptor;

    @Resource(name = "rightsInterceptor")
    private RightsInterceptor rightsInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
        registry.addResourceHandler("/frame/**").addResourceLocations("classpath:/static/frame/");
        registry.addResourceHandler("/style/**").addResourceLocations("classpath:/static/style/");
        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("classpath:/static/fonts/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/files/**").addResourceLocations("file:///" + fileConfig.getRoot() + File.separator);
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {


        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("PUT", "DELETE", "GET", "POST", "OPTIONS")
                .allowedHeaders("X-Requested-with", "Content-Type", "authorization", "jsonType", "json-type", JwtsUtil.ADMIN_HEADER_KEY,"Adminauthorization")
                .exposedHeaders("Authorization", "X-Requested-With", "Content-Type", "jsonType", "json-type", JwtsUtil.ADMIN_HEADER_KEY,"Adminauthorization")
                .allowCredentials(false).maxAge(3600);

    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(optionsInterceptor).addPathPatterns("/**");

        //"/sampleReport/getViewUrlPath", "/v1/**",
        if (!debug) {

            registry.addInterceptor(loginInterceptor).addPathPatterns("/**")
                    .excludePathPatterns(
                            "/**/exportExcelByCode/*",
                            "/uploadHtml",
                            "/companyIntegralLog/exportMonthExcelByCode/*",
                            "/dataFilling/exportOneExcelByCode/*",
                            "downloadFileByCode/**",
                            "/code/*",
                            "/screen/**",
                            "/dataFilling/exportByCode/*",
                            "/v2/api-docs/**",
                            "/login", "/files/**", "/imageCode", "/html2Img", "/swagger-resources/**", "/doc.html", "/webjars/**");


            if (!rightsDebug) {

                //"/sampleReport/getViewUrlPath", "/v1/**",
                registry.addInterceptor(rightsInterceptor)
                        .addPathPatterns("/**")
                        .excludePathPatterns(
                                "/**/exportExcelByCode/*",
                                "/uploadHtml",
                                "/companyIntegralLog/exportMonthExcelByCode/*",
                                "downloadFileByCode/**",
                                "/dataFilling/exportOneExcelByCode/*",
                                "/screen/**",
                                "/dataFilling/exportByCode/*",
                                "/code/*",
                                "/v2/api-docs/**",
                                "/login", "/files/**", "/imageCode", "/html2Img", "/noRights/**", "/doc.html", "/swagger-resources/**", "/webjars/**");

            }


        }

    }


}
